package com.nomi.blog.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    @Autowired
    private JwtInterceptor jwtInterceptor;  //  JWT拦截器

    // 添加拦截器
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
/*        registry.addInterceptor(jwtInterceptor)
                //.addPathPatterns("/user/info", "/user/updateInfo","/user/upload") // 需要验证的接口
                .addPathPatterns("/**")
                .excludePathPatterns("/user/register",
                        "/user/login",
                        "/user/resetPassword",
                        "/user/info/**",
                        "/userinfo/get/**",
                        "/article/latest",
                        "/article/latestWithoutPage",
                        "/article/hotTags",
                        "/article/hotTagsWithoutPage",
                        "/article/hotArticles",
                        "/article/hotArticlesWithoutPage",
                        "/article/archives",
                        "/article/archivesWithoutPage",
                        "/article/tag/**",
                        "/article/tagWithoutPage/**",
                        "/article/archiveType/**",
                        "/article/archiveTypeWithoutPage/**",
                        "/article/allTags",
                        "/article/timeArchives",
                        "/article/detail/**",
                        "/article/increaseViewCount/**"
                ); // 排除的接口*/

        registry.addInterceptor(jwtInterceptor)
                //.addPathPatterns("/user/info", "/user/updateInfo","/user/upload") // 需要验证的接口
                .addPathPatterns("/user/info",
                        "/user/upload",
                        "/userInfo/update",
                        "/userInfo/get/**",
                        "/article/create",
                        "/article/update",
                        "/article/publish/**",
                        "/article/saveDraft",
                        "/article/review/**",
                        "/article/user/**",
                        "/follower/list/**",
                        "/follower/follow/**",
                        "/following/**"
                        );
                //.excludePathPatterns("/**"); // 排除的接口
    }

    // 配置跨域请求
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")  // 匹配所有接口路径
                .allowedOrigins("*") // 允许所有来源
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS") // 支持的HTTP方法
                .allowedHeaders("*") // 允许所有请求头
                .exposedHeaders("Authorization") // 暴露给前端的响应头
                .allowCredentials(false) // 是否允许发送 Cookie
                .maxAge(5000); // 预检请求的有效期（单位：秒）
    }

//    @Override
//    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//        // 保留 Spring 默认的消息转换器（包括 Jackson）
//        WebMvcConfigurer.super.configureMessageConverters(converters);
//    }

    // 配置静态资源映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/avatars/**")
                .addResourceLocations("file:src/main/resources/uploads/avatars/");
        registry.addResourceHandler("/articles/**")
                .addResourceLocations("file:src/main/resources./articles/");
    }
}
